package com.sne.airways.system.filters;

import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-17
 * Time: 下午1:07
 * To change this template use File | Settings | File Templates.
 */
public class SessionFilter implements Filter {
    private Logger logger = Logger.getLogger(getClass());

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        String url = request.getRequestURI();
        for(String c : PowerFilter.noFilterPages){
            if(url.indexOf(c) > 0){
                filterChain.doFilter(servletRequest,servletResponse);
                return;
            }
        }

        String requestedSessionId = request.getRequestedSessionId();
        HttpSession session = request.getSession();
        if(requestedSessionId.equals(session.getId())){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
//        Session session = AppUtils.getSessionCache(session.getId());
//        if(_map == null){
//            Map<String,Object> map = SessionCacheService.getSessionCache(requestedSessionId);
//            if(map == null){
//                filterChain.doFilter(servletRequest,servletResponse);
//                return;
//            }
//            logger.info("开始故障转移...");
//            SessionCacheService.initSessionCache(session.getId(),map);
//        }

        filterChain.doFilter(servletRequest,servletResponse);
    }

    public void destroy() {

    }
}

